package com.neusoft.neu24.nep.service.impl;

import com.neusoft.neu24.nep.entity.CommonResult;
import com.neusoft.neu24.nep.entity.Role;
import com.neusoft.neu24.nep.entity.Sysnode;
import com.neusoft.neu24.nep.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    DiscoveryClient client;

    @Override
    public CommonResult selectById(Integer roleid) {

        List<ServiceInstance> instances = client.getInstances("service-provider");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+":"+instance.getPort()+":"+instance.getUri());
        }
        ServiceInstance instance = instances.get(0);

        CommonResult<List<Sysnode>> result =  WebClient.create(instance.getUri().toString())
                .get()
                .uri("/sn/get/byroleid/" + roleid)
                .retrieve()
                .bodyToMono(CommonResult.class)
                .block();
        if (result.getCode() == 200){
            Role role =new Role();
            role.setId(roleid);
            role.setSysnodes( result.getResult() );

            return CommonResult.sucess(role);

        }else {
            return CommonResult.filure();
        }


    }
}
